package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.infos.ClubLinkListModel;
import com.badibadi.infos.ClubLinkModel;
import com.badibadi.infos.Record_Info_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 俱乐部的其它空间
 * 
 * @author Administrator
 * 
 */
public class ClubOtherSpaceActivity extends BaseActivity {

	protected static final String TAG = "ClubOtherSpaceActivity";
	String cid;
	ListView lv_club_other_space;
	clubOtherSpaceAdapter clAdapter;
	ClubLinkModel clubLinkModel;
	private List<ClubLinkListModel> clubLinkListModels;
	// int[] img = { R.drawable.logo_qq, R.drawable.logo_sinaweibo,
	// R.drawable.logo_facebook, R.drawable.logo_twitter,
	// R.drawable.logo_googleplus, R.drawable.noimage };
	//
	// String[] name = { "qq", "sina", "facebook", "twitter", "google+",
	// getResources().getString(R.string.l_xb52)
	//
	// };

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ClubOtherSpaceActivity.this);
				try {
					Utils.showMessage(ClubOtherSpaceActivity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:
				Utils.ExitPrgress(ClubOtherSpaceActivity.this);
				try {
					clubLinkListModels = Fanhui_num(clubLinkModel);
					clAdapter.notifyDataSetChanged();
					initListView();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				Utils.ExitPrgress(ClubOtherSpaceActivity.this);
				try {
					Utils.showMessage(ClubOtherSpaceActivity.this,
							getResources().getString(R.string.l_xb49));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_club_other_space);

		cid = getIntent().getStringExtra("cid");
		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});

		lv_club_other_space = (ListView) this
				.findViewById(R.id.club_other_space);

		clubLinkModel = new ClubLinkModel();
		clubLinkListModels = new ArrayList<ClubLinkListModel>();
		clAdapter = new clubOtherSpaceAdapter();
		lv_club_other_space.setAdapter(clAdapter);

		// lv_club_other_space.setAdapter(clAdapter);
		clubLink(cid);

	}

	protected void initListView() {

		clAdapter.notifyDataSetInvalidated();
	}

	private List<ClubLinkListModel> Fanhui_num(ClubLinkModel linkModel) {
		ClubLinkListModel listModel = null;
		List<ClubLinkListModel> clubLinkListModels = new ArrayList<ClubLinkListModel>();
		if (!StringUtil.isNullOrEmpty(linkModel.getFacebook_url())
				&& !linkModel.getFacebook_url().equals("null")) {
			listModel = new ClubLinkListModel();
			listModel.setSpace_Link(linkModel.getFacebook_url());
			listModel.setSpace_name("facebook");
			listModel.setSpace_logo(R.drawable.logo_facebook);
			clubLinkListModels.add(listModel);
		}
		if (!StringUtil.isNullOrEmpty(linkModel.getGoogle_url())
				&& !linkModel.getGoogle_url().equals("null")) {
			listModel = new ClubLinkListModel();
			listModel.setSpace_Link(linkModel.getGoogle_url());
			listModel.setSpace_name("google+");
			listModel.setSpace_logo(R.drawable.logo_googleplus);
			clubLinkListModels.add(listModel);
		}
		if (!StringUtil.isNullOrEmpty(linkModel.getOther_url())
				&& !linkModel.getOther_url().equals("null")) {
			listModel = new ClubLinkListModel();
			listModel.setSpace_Link(linkModel.getOther_url());
			listModel.setSpace_name(getResources().getString(R.string.l_xb52));
			listModel.setSpace_logo(R.drawable.noimage);
			clubLinkListModels.add(listModel);
		}
		if (!StringUtil.isNullOrEmpty(linkModel.getQq_url())
				&& !linkModel.getQq_url().equals("null")) {
			listModel = new ClubLinkListModel();
			listModel.setSpace_Link(linkModel.getQq_url());
			listModel.setSpace_name("qq");
			listModel.setSpace_logo(R.drawable.logo_qq);
			clubLinkListModels.add(listModel);
		}
		if (!StringUtil.isNullOrEmpty(linkModel.getSina_url())
				&& !linkModel.getSina_url().equals("null")) {
			listModel = new ClubLinkListModel();
			listModel.setSpace_Link(linkModel.getSina_url());
			listModel.setSpace_name("sina");
			listModel.setSpace_logo(R.drawable.logo_sinaweibo);
			clubLinkListModels.add(listModel);
		}
		if (!StringUtil.isNullOrEmpty(linkModel.getTwitter_url())
				&& !linkModel.getTwitter_url().equals("null")) {
			listModel = new ClubLinkListModel();
			listModel.setSpace_Link(linkModel.getTwitter_url());
			listModel.setSpace_name("twitter");
			listModel.setSpace_logo(R.drawable.logo_twitter);
			clubLinkListModels.add(listModel);
		}
		
		return clubLinkListModels;

	}

	/**
	 * 
	 * @param clubId
	 *            俱乐部的id
	 */
	private void clubLink(final String clubId) {
		// TODO Auto-generated method stub

		Utils.showPrgress(ClubOtherSpaceActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("cid", clubId);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/clubLink");
				// String content = Utils.getRequest(Constants.BadiUrl
				// + "record_info?cid=" + cid);
				//    (Constants.BadiUrl + "friends?uid=" + id
				// + "&page=" + page + "&pageNum=" + 10);
				if (content != null) {
					Results results = Utils.checkResult_NNN(
							ClubOtherSpaceActivity.this, content);
//					   
//					   
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet() == true) {
//						   
						try {
							clubLinkModel = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), ClubLinkModel.class);

							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {

					handler.sendEmptyMessage(1);
				}
			}
		});

	}

	/**
	 * 俱乐部的其它空间
	 * 
	 * @author Administrator
	 * 
	 */
	private class clubOtherSpaceAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return clubLinkListModels.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return clubLinkListModels.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		// {
		// "other_url": "",
		// "qq_url": "",
		// "sina_url": "",
		// "facebook_url": "",
		// "twitter_url": "",
		// "google_url": ""
		// }
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = LayoutInflater
					.from(ClubOtherSpaceActivity.this);
			View view = inflater.inflate(R.layout.club_item, null);

			ImageView iv_logo = (ImageView) view.findViewById(R.id.logo);

			// if (position != 5) {
			// iv_logo.setImageResource(img[position]);
			// }
			TextView names = (TextView) view.findViewById(R.id.name);
			iv_logo.setImageResource(clubLinkListModels.get(position)
					.getSpace_logo());
			names.setText(clubLinkListModels.get(position).getSpace_name());
			// names.setText("" + name[position]);
			TextView Link = (TextView) view.findViewById(R.id.Link);
			Link.setText(clubLinkListModels.get(position).getSpace_Link());
			Link.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (!Utils.getUid(ClubOtherSpaceActivity.this).equals("")) {
					EnterWebView(clubLinkListModels.get(position)
							.getSpace_Link());
					}else {
						Toast.makeText(ClubOtherSpaceActivity.this,
								getResources().getString(R.string.l_xb10), 0)
								.show();
					}
				}
			});
			// if (position == 0) {
			//
			// if (clubLinkModel.getQq_url() != null) {
			// Link.setText("" + clubLinkModel.getQq_url());
			// Link.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// EnterWebView(clubLinkModel.getQq_url());
			// }
			// });
			// }
			//
			// }
			// if (position == 1) {
			//
			// if (clubLinkModel.getSina_url() != null) {
			// Link.setText("" + clubLinkModel.getSina_url());
			// Link.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// EnterWebView(clubLinkModel.getSina_url());
			// }
			// });
			// }
			//
			// }
			// if (position == 2) {
			//
			// if (clubLinkModel.getFacebook_url() != null) {
			// Link.setText("" + clubLinkModel.getFacebook_url());
			// Link.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// EnterWebView(clubLinkModel.getFacebook_url());
			// }
			// });
			// }
			//
			// }
			// if (position == 3) {
			//
			// if (clubLinkModel.getTwitter_url() != null) {
			// Link.setText("" + clubLinkModel.getTwitter_url());
			// Link.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// EnterWebView(clubLinkModel.getTwitter_url());
			// }
			// });
			// }
			//
			// }
			// if (position == 4) {
			//
			// if (clubLinkModel.getGoogle_url() != null) {
			// Link.setText("" + clubLinkModel.getGoogle_url());
			// Link.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// EnterWebView(clubLinkModel.getGoogle_url());
			// }
			// });
			// }
			//
			// }
			// if (position == 5) {
			//
			// if (clubLinkModel.getOther_url() != null) {
			// Link.setText("" + clubLinkModel.getOther_url());
			// Link.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// EnterWebView(clubLinkModel.getOther_url());
			// }
			// });
			// }
			//
			// }
			return view;
		}

	}

}
